package com.animationsp.acs.action;

import org.apache.commons.codec.binary.Base64;

import java.io.File;
import java.io.UnsupportedEncodingException;

import org.json.JSONException;

import com.animationsp.acs.util.Constant;
import com.animationsp.acs.util.ErrorMap;
import com.animationsp.acs.util.MyActionSupport;
import com.qiniu.api.auth.AuthException;
import com.qiniu.api.auth.digest.Mac;
import com.qiniu.api.config.Config;
import com.qiniu.api.io.IoApi;
import com.qiniu.api.io.PutExtra;
import com.qiniu.api.io.PutRet;
import com.qiniu.api.rs.PutPolicy;

public class UploadAction extends MyActionSupport {
	// 上传请求的输入
	private String key;;
	private File file;

	// 上传请求的输出
	private String uploadToken;

	// upload callback 的输入
	private String upload_ret;
	private String code; // 上传失败错误号
	private String error; // 错误信息

	// upload callback 的输出

	public String upload() throws Exception {
		Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY);
		String bucketName = Constant.BUCKET;
		PutPolicy putPolicy = new PutPolicy(bucketName);
		putPolicy.endUser = this.getUsername();
		System.out.println("upload: "+putPolicy.endUser);
		putPolicy.returnUrl = "http://" + Constant.DOMAIN + "/acs/uploadCallback.action";
		putPolicy.returnBody = "{'bucket':$(bucket),'etag':$(etag),'key':$(key),'mimeType':$(mimeType),'fsize':$(fsize),'endUser':$(endUser),'fname':$(fname),'fromUrl':$(x:fromUrl)}";
		uploadToken = putPolicy.token(mac);

		/*
		 * 上传文件 PutExtra extra = new PutExtra(); key = "/"+this.getUsername();
		 * File localFile = new File("d:\\d.txt"); PutRet ret =
		 * IoApi.putFile(uploadToken, key, localFile, extra);
		 */

		return SUCCESS;
	}

	public String uploadCallback() throws Exception {
		if (code != null && code.length() != 0) {
			this.setErr(ErrorMap.ERROR_MAP.get(code) + error);
		}else{
			System.out.println("upload callback upload_ret: " + upload_ret);
			upload_ret.replaceAll("-", "+");
			upload_ret.replaceAll("_", "/");
			upload_ret = new String(Base64.decodeBase64(upload_ret),"utf-8");
			
			System.out.println(System.getProperty("file.encoding"));
			System.out.println("upload callback upload_ret: " + upload_ret);
		}
		
		
		return SUCCESS;
	}

	public static void main(String[] args) {
		/*
		Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY);
		String bucketName = "aspcs";
		PutPolicy putPolicy = new PutPolicy(bucketName);
		String uptoken;
		try {
			uptoken = putPolicy.token(mac);
			PutExtra extra = new PutExtra();
			String key = "<key>";
			File localFile = new File("d:\\d.txt");
			PutRet ret = IoApi.putFile(uptoken, key, localFile, extra);
			System.out.println("ret: " + ret.statusCode + " " + ret.response);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AuthException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
		
		System.out.println(System.getProperty("file.encoding"));
		String text = "大家好";
		System.out.println(text);
		String upload_ret = "eydidWNrZXQnOiJhc3BjcyIsJ2V0YWcnOiJGcm9GcnA5NHlYUTJmR29XOGQ2NEtYamNSLXAzIiwna2V5JzoiXHUwMDNja2V5XHUwMDNlIiwnbWltZVR5cGUnOiJ0ZXh0L3BsYWluIiwnZnNpemUnOjMzMSwnZW5kVXNlcic6ImFkbWluIiwnZm5hbWUnOiJvYWJ1Zy50eHQifQ==";
		upload_ret.replaceAll("-", "+");
		upload_ret.replaceAll("_", "/");
		try {
			upload_ret = new String(Base64.decodeBase64(upload_ret),"utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("upload callback upload_ret: " + upload_ret);
	}

	public String getUploadToken() {
		return uploadToken;
	}

	public void setUploadToken(String uploadToken) {
		this.uploadToken = uploadToken;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getUpload_ret() {
		return upload_ret;
	}

	public void setUpload_ret(String upload_ret) {
		this.upload_ret = upload_ret;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

}
